const { defineConfig } = require('@vue/cli-service')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const webpack = require('webpack')
const path = require('path')
 
let cesiumSource = './node_modules/cesium/Source/' //按理说应该是未打包的
// 此处必需，因为高版本cesium没有这个Workers报错。必写-1
const cesiumWorkers = '../Build/Cesium/Workers'

module.exports = defineConfig({
  transpileDependencies: true,
  publicPath: './',
  devServer:{
    open: false,/*自动打开浏览器*/
    host: '0.0.0.0', /*使所有的IP都可访问*/
    port: 8081,
    /*使用代理*/
    proxy:{
      '/api': {
        target: 'https://10.112.38.20:30001', // 后端接口地址
        changeOrigin: true, // needed for virtual hosted sites
        withCredentials: true,
        pathRewrite: {
          '^/api': '' // rewrite path
        }        
      },
      // '/ws': { // 这里的 '/ws' 是你前端请求 WebSocket 时的路径前缀，根据实际情况修改 
      //   target: 'http://10.112.51.79:38414', // 后端 WebSocket 服务的真实地址 
      //   ws: true, // 表明是代理 WebSocket 连接 
      //   changeOrigin: true // 允许跨域 }
      // }
    },

  },
  pluginOptions:{
    'style-resources-loader':{
      preProcessor:'scss',
      patterns:[]
    }
  },
  configureWebpack: {
    output: {
      sourcePrefix: ' '
    },
    amd: {
      toUrlUndefined: true
    },
    resolve: {
      alias: {
        '@': path.resolve('src'),
        //'cesium': path.resolve(__dirname, cesiumSource),//如果有依赖cesium的库就需要下面这种。
        'cesium': path.resolve(__dirname, './node_modules/cesium/')
      },
      // 参考官方github的，我不太懂webpack，所以都不知道咋解决https zlib问题  必写-2
      fallback: { "https": false, "zlib": false, "http": false, "url": false },
    },
    plugins: [
      // 对于webpack版本此处有不同配置，webpack低版本5.x执行下面4行注释的代码，对于webpack高版本9.x及以上，patterns是可以的。
      new CopyWebpackPlugin({
        patterns: [
          { from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' },
          { from: path.join(cesiumSource, 'Assets'), to: 'Assets' },
          { from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' },
          { from: path.join(cesiumSource, 'ThirdParty'), to: 'ThirdParty' },//需要整个ThirdParty
        ],
      }),
      // new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Workers'), to: 'Workers'}]),
      // new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Assets'), to: 'Assets'}]),
      // new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Widgets'), to: 'Widgets'}]),
      // new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'ThirdParty'), to: 'ThirdParty'}]),
      new webpack.DefinePlugin({
        CESIUM_BASE_URL: JSON.stringify('./')
        // CopyWebpackPlugin from的绝对路径
      })
    ],
    module: {
      rules: [
        {
          test: /\.js$/,
          use: {
            loader: "@open-wc/webpack-import-meta-loader",
          },
        },
        {
          test: /\.(glb|gltf)?$/,
          use: {
            loader: "url-loader",
          },
        },
      ],
    },
    externals: {
        'BMap': 'BMap',
        'BMap_Symbol_SHAPE_POINT':'BMap_Symbol_SHAPE_POINT'
    }
  }
})
